package lovo.practice_7_14;

import lovo.Util.Util;

import javax.swing.*;
import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel extends JPanel implements ActionListener {
    private J189_7_14 frame;
    private JButton jButtonTrans;
    private JTextField jTextFieldwho;
    private JTextField jTextFieldhow;
    private JButton jButtonBack;
    private Icon imgback = Util.getimagesIcon("/Images/buttonOff.jpg");

    public TransPanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonTrans = new JButton("转账");
        jButtonTrans.setBounds(200, 200, 140, 50);
        this.add(jButtonTrans);

        jTextFieldwho = new JTextField();
        jTextFieldwho.setBounds(400, 200, 140, 50);
        this.add(jTextFieldwho);

        jTextFieldhow = new JTextField();
        jTextFieldhow.setBounds(400, 400, 140, 50);
        this.add(jTextFieldhow);

        jButtonBack = new JButton(imgback);
        jButtonBack.setBounds(600, 600, 140, 50);
        this.add(jButtonBack);
        jButtonTrans.addActionListener(this);
        jTextFieldwho.addActionListener(this);
        jTextFieldhow.addActionListener(this);
        jButtonBack.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButtonTrans) {
            // 获取输入的对方账号
            String otherAccount = jTextFieldwho.getText();
            double blance = 0;
            try {
                // 获取输入的转账金额
                blance = Double.parseDouble(jTextFieldhow.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;
            }

            // 当前登录的账户
            DBUtil db = new DBUtil();
            User loginUser = db.getById(frame.getUsers().getAccount());

            DBUtil db1 = new DBUtil();
            User otherUser = db1.getById(otherAccount);
//            ArrayList<User> users = frame.getUsers();
//
//            boolean isExist = false;
//            for(int i = 0;i < users.size();i++){
//                if(users.get(i).getAccount().equals(otherAccount)){
//                    isExist = true;
//                    otherUser = users.get(i);
//                    break;
//                }
//            }
            // 判断账号是否存在
            if (otherUser == null) {
                JOptionPane.showMessageDialog(this, "查无此人");
                return;
            }
            // 判断是否转给自己
            if (loginUser.getAccount().equals(otherAccount)) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                return;
            }
            // 判断余额是否足够
            if (blance > loginUser.getBalance()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
            DBUtil dbUtil = new DBUtil();
            boolean flag = dbUtil.testTransition(loginUser, otherUser);
            if (flag) {
                // 当期账户扣钱
                loginUser.setBalance(loginUser.getBalance() - blance);
                // 对方账户加钱
                otherUser.setBalance(otherUser.getBalance() + blance);
                db.update(loginUser);
                db1.update(otherUser);
                JOptionPane.showMessageDialog(this, "转账成功");
                frame.switchPanel("menu");
            } else {
                return;
            }

        }
        if (e.getSource() == jButtonBack) {
            frame.switchPanel("mune");
        }
    }
}
